package com.dididoctor.patient.Activity.MakeCall;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Doctor.Doctordetail.Prdbuy;
import com.dididoctor.patient.Activity.Doctor.Doctordetail.Prdetail;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.*;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： MakeCallPresenter
 * @包名称： com.dididoctor.patient.Activity.MakeCall
 * @创建人： zhanjunjie
 * @创建时间：2016/4/11 16:21
 * @修改人：
 * @修改时间：2016/4/11 16:21
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class MakeCallPresenter extends BasePresenter {
    private MakeCallView view;

    public MakeCallPresenter(Context context, MakeCallView view) {
        super(context, view);
        this.view = view;
    }

    /**
     * 获取产品价格
     *
     * @param type
     */
    void getprdetail(String type) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        BusinessClient.post(ConstantValue.prdetail, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Prdetail prdetail = new Prdetail();
                    prdetail.setPrdId(com.dididoctor.patient.Utils.Util.toString((Double) response.getObject("prdId")));
                    prdetail.setName(com.dididoctor.patient.Utils.Util.toString(response.getString("name")));
                    prdetail.setWebUrl(com.dididoctor.patient.Utils.Util.toString(response.getString("webUrl")));
                    prdetail.setPrize((Double) response.getObject("prize") + "");
                    prdetail.setMemo(com.dididoctor.patient.Utils.Util.toString(response.getString("memo")));
                    view.getprdetail(prdetail);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getprdetailfail();
            }
        });
    }


    public void paysuccess(String type, String payId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.paysuccess, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.paysuccess();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
            }
        });
    }

    public void bookingcall(String phone, String price, String descr, String doctorId, String hosId, String type) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("phone", phone);
        params.put("price", price);
        params.put("diseId", descr);
        params.put("doctorId", doctorId);
        params.put("hosId", hosId);
        params.put("type", type);
        params.put("payWay", "1");
        BusinessClient.post(ConstantValue.bookingcall, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Prdbuy prdbuy = new Prdbuy();
                    prdbuy.setPayId(com.dididoctor.patient.Utils.Util.toString(response.getString("payId")));
                    prdbuy.setSign(com.dididoctor.patient.Utils.Util.toString(response.getString("sign")));
                    prdbuy.setNonceStr(com.dididoctor.patient.Utils.Util.toString(response.getString("nonceStr")));
                    prdbuy.setDescr(com.dididoctor.patient.Utils.Util.toString(response.getString("descr")));
                    prdbuy.setNotifyurl(com.dididoctor.patient.Utils.Util.toString(response.getString("notifyurl")));
                    prdbuy.setTimeStamp(com.dididoctor.patient.Utils.Util.toString(response.getString("timeStamp")));
                    prdbuy.setPartnerId(com.dididoctor.patient.Utils.Util.toString(response.getString("partnerId")));
                    prdbuy.setPrepayId(com.dididoctor.patient.Utils.Util.toString(response.getString("prepayId")));
                    prdbuy.set_input_charset(com.dididoctor.patient.Utils.Util.toString(response.getString("_input_charset")));
                    prdbuy.setSubject(com.dididoctor.patient.Utils.Util.toString(response.getString("subject")));
                    prdbuy.setTotal_fee(com.dididoctor.patient.Utils.Util.toString(response.getString("total_fee")));
                    prdbuy.setSign_type(com.dididoctor.patient.Utils.Util.toString(response.getString("sign_type")));
                    prdbuy.setService(com.dididoctor.patient.Utils.Util.toString(response.getString("service")));
                    prdbuy.setNotify_url(com.dididoctor.patient.Utils.Util.toString(response.getString("notify_url")));
                    prdbuy.setPartner(com.dididoctor.patient.Utils.Util.toString(response.getString("partner")));
                    prdbuy.setSeller_id(com.dididoctor.patient.Utils.Util.toString(response.getString("seller_id")));
                    prdbuy.setOut_trade_no(com.dididoctor.patient.Utils.Util.toString(response.getString("out_trade_no")));
                    prdbuy.setPayment_type(com.dididoctor.patient.Utils.Util.toString(response.getString("payment_type")));
                    prdbuy.setRecId(com.dididoctor.patient.Utils.Util.toString(response.getString("recId")));
                    prdbuy.setPrivateKey(com.dididoctor.patient.Utils.Util.toString(response.getString("privateKey")));
                    view.bookingcall(prdbuy);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.bookingcallfail();
            }
        });
    }


}
